#ifndef __MY_HOT__ 
#define __MY_HOT__ 
#define <unistd.h>
#define "data.hpp"

extern cloud::DataManager *_data;

namespace cloud 
{
    class HotMananger 
    {
    private:
        std::string _back_dir;
        std::string _pack_dir;
        std::string _pack_suffix;
        int _hot__time; 
    private:
        bool HotJudge(const std::string &filename) 
        {
            FileUtil fu(filename);
            time_t last_time = fu.LastATime();
            time_t cur_time = time(NULL);
            if(cur_time - last_atime > _hot_time) 
            {
                return true; 
            }
            return false; 
        }
    public:
        HotManager() 
        {
            Config *config = Config::GetInstance();
            _back_dir = config->GetBackDie();
            _pack_dir = config->GetPackDir();
            _pack_suffix = config->GetPackFileSuffix();
            _hot_time = config->GetHotTime();
            FileUtil tmp1(_back_dir);
            FileUtil tmp2(_pack_dir);
            tmp1.CreateDirectory();
            tmp2.CreateDirectory(); 
        }

        bool RunModule() 
        {
            while(1) 
            {
                FileUtil fu(_back_dir);
                std::vector<std::string> arry;
                fu.ScanfDirectory(&arry);
                for(auto &a : arry) 
                {
                    if(HotJudge(a) == false) 
                    {
                        continue; 
                    }
                    BackupInfo bi;
                    if(_data->GetOneByRealPath(a, &bi) == false) 
                    {
                        bi.NewBackInfo(a);
                    }
                    FileUtil tmp(a);
                    tmp.Compress(bi.pack_path);
                    tmp.Remove();
                    bi.pack_flag = true; 
                    _data->Update(bi); 
                }
                usleep(1000); 
            }
            return true; 
        }
    };
}


#endif 
